#!/bin/sh
# 
# chkconfig: 345 99 01
# description: Nagios network monitor
#
# File : nagios
#
# Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
# 
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#
  
status_nagios ()
{

	if test -x $NagiosCGI/daemonchk.cgi; then
		if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
		        return 0
		else
			return 1
		fi
	else
		if ps -p $NagiosPID > /dev/null 2>&1; then
		        return 0
		else
			return 1
		fi
	fi

	return 1
}


printstatus_nagios()
{

	if status_nagios $1 $2; then
		echo "nagios (pid $NagiosPID) is running..."
	else
		echo "nagios is not running"
	fi
}


killproc_nagios ()
{

	kill $2 $NagiosPID

}


pid_nagios ()
{

	if test ! -f $NagiosRunFile; then
		echo "No lock file found in $NagiosRunFile"
		exit 1
	fi

	NagiosPID=`head -n 1 $NagiosRunFile`
}

# Download config from ast
ast_config_dl ()
{
#	echo "Backing up AST config."
#	cp ${NAGIOS_COLO} ${NAGIOS_COLO}.backup
	echo -n "Downloading config from ${AST_URL}:"
	${CURL} ${AST_URL} > ${NAGIOS_COLO} 2>/dev/null
	if [ $? -eq 0 ]; then
		echo " done."
	else
		echo "Fetch AST config failed."
	fi
}


# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

prefix=/usr/local/nagios3
exec_prefix=${prefix}
NagiosBin=${exec_prefix}/bin/nagios
NagiosCfgFile=${prefix}/etc/nagios.cfg
NagiosStatusFile=${prefix}/var/status.dat
NagiosRetentionFile=${prefix}/var/retention.dat
NagiosCommandFile=${prefix}/var/rw/nagios.cmd
NagiosVarDir=${prefix}/var
NagiosRunFile=${prefix}/var/nagios.lock
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=${exec_prefix}/sbin
NagiosUser=nagios
NagiosGroup=nagios

# AST server
AST_SVR="ast2.example.com"
# hostname
HOSTNAME=`/bin/hostname -f`
# ast url to fetch config
AST_URL="https://${AST_SVR}/nagios_host_groups/build_hostgroup/1/?src=${HOSTNAME}"
# detination file
NAGIOS_COLO=${prefix}/etc/colo/colo.cfg
# nagios cfg
NAGIOS_CFG=${prefix}/etc/nagios/nagios.cfg
# CURL and options
CURL="/usr/bin/curl -k -s -u USERNAME:PASSWORD"
# Restart log
STARTUP_CHECK="${prefix}/startup_check.log"

# Output when config has error
ast_error_output ()
{
	echo "CONFIG ERROR!  Start aborted.  Check your Nagios configuration."
	echo "Copy bad config to .bad"
	cp ${NAGIOS_COLO} ${NAGIOS_COLO}.bad
	echo "Reverting to last good config"
	cp ${NAGIOS_COLO}.good ${NAGIOS_COLO}
	echo "See the error with 'cat ${STARTUP_CHECK}'"
	echo "You can restart nagios without downloading AST by doing '$0 restart -no_ast'"

}

# Check that nagios exists.
if [ ! -f $NagiosBin ]; then
    echo "Executable file $NagiosBin not found.  Exiting."
    exit 1
fi

# Check that nagios.cfg exists.
if [ ! -f $NagiosCfgFile ]; then
    echo "Configuration file $NagiosCfgFile not found.  Exiting."
    exit 1
fi

# Capture second parameter
AST_FLAG=$2
          
# See how we were called.
case "$1" in

	start)
                if [ "$AST_FLAG" != "-no_ast" ]; then
			ast_config_dl
		fi
		echo -n "Starting nagios:"
		$NagiosBin -v $NagiosCfgFile > ${STARTUP_CHECK} 2>&1;
		if [ $? -eq 0 ]; then
                        cp ${NAGIOS_COLO} ${NAGIOS_COLO}.good
			su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
			rm -f $NagiosCommandFile
			touch $NagiosRunFile
			chown $NagiosUser:$NagiosGroup $NagiosRunFile
			$NagiosBin -d $NagiosCfgFile
			if [ -d $NagiosLockDir ]; then touch $NagiosLockDir/$NagiosLockFile; fi
			echo " done."
			/etc/init.d/nsca restart
			/etc/init.d/nsca_5668 restart
			exit 0
		else
			ast_error_output
			exit 1
		fi
		;;

	stop)
		echo -n "Stopping nagios: "

		pid_nagios
		killproc_nagios nagios

 		# now we have to wait for nagios to exit and remove its
 		# own NagiosRunFile, otherwise a following "start" could
 		# happen, and then the exiting nagios will remove the
 		# new NagiosRunFile, allowing multiple nagios daemons
 		# to (sooner or later) run - John Sellens
		#echo -n 'Waiting for nagios to exit .'
 		for i in 1 2 3 4 5 6 7 8 9 10 ; do
 		    if status_nagios > /dev/null; then
 			echo -n '.'
 			sleep 1
 		    else
 			break
 		    fi
 		done
 		if status_nagios > /dev/null; then
 		    echo ''
 		    echo 'Warning - nagios did not exit in a timely manner'
 		else
 		    echo 'done.'
 		fi

		rm -f $NagiosStatusFile $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
		;;

	status)
		pid_nagios
		printstatus_nagios nagios
		;;

	checkconfig)
		printf "Running configuration check..."
		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo " OK."
		else
			echo " CONFIG ERROR!  Check your Nagios configuration."
			exit 1
		fi
		;;

	restart)
		printf "Running configuration check..."
		$NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo "done."
			$0 stop $AST_FLAG
			$0 start $AST_FLAG
		else
			echo " CONFIG ERROR!  Restart aborted.  Check your Nagios configuration."
			echo "Run this command manually to see error message:"
			echo "$NagiosBin -v $NagiosCfgFile"
			echo "Or copy ${NAGIOS_COLO}.good to ${NAGIOS_COLO} to use last good config."
			exit 1
		fi
		;;

	reload|force-reload)
		printf "Running configuration check..."
		$NagiosBin -v $NagiosCfgFile > ${STARTUP_CHECK} 2>&1;
		if [ $? -eq 0 ]; then
			echo "done."
			if test ! -f $NagiosRunFile; then
                        	cp ${NAGIOS_COLO} ${NAGIOS_COLO}.good
				$0 start $AST_FLAG
			else
				pid_nagios
				if status_nagios > /dev/null; then
			 		ast_config_dl
					$NagiosBin -v $NagiosCfgFile > ${STARTUP_CHECK} 2>&1;
					if [ $? -eq 0 ]; then
						printf "Reloading nagios configuration..."
						killproc_nagios nagios -HUP
						echo "done"
					else
						ast_error_output
					fi
				else
					$0 stop $AST_FLAG
					$0 start $AST_FLAG
				fi
			fi
		else
			echo " CONFIG ERROR!  Reload aborted.  Check your Nagios configuration."
			exit 1
		fi
		;;

	*)
		echo "Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig}"
		exit 1
		;;

esac
  
# End of this script
